package 
{
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author DarkGoodWIN
	 */
	public class StringUtils
	{
		public static var MaxCharCode: int = 146;
		
		public static function ByteToChar(B: int): String
		{
			if (B == 0)
				return '0'
			else if (B == 1)
				return '1'
			else if (B == 2)
				return '2'
			else if (B == 3)
				return '3'
			else if (B == 4)
				return '4'
			else if (B == 5)
				return '5'
			else if (B == 6)
				return '6'
			else if (B == 7)
				return '7'
			else if (B == 8)
				return '8'
			else if (B == 9)
				return '9'
			else if (B == 10)
				return 'q'
			else if (B == 11)
				return 'w'
			else if (B == 12)
				return 'e'
			else if (B == 13)
				return 'r'
			else if (B == 14)
				return 't'
			else if (B == 15)
				return 'y'
			else if (B == 16)
				return 'u'
			else if (B == 17)
				return 'i'
			else if (B == 18)
				return 'o'
			else if (B == 19)
				return 'p'
			else if (B == 20)
				return '['
			else if (B == 21)
				return ']'
			else if (B == 22)
				return 'a'
			else if (B == 23)
				return 's'
			else if (B == 24)
				return 'd'
			else if (B == 25)
				return 'f'
			else if (B == 26)
				return 'g'
			else if (B == 27)
				return 'h'
			else if (B == 28)
				return 'j'
			else if (B == 29)
				return 'k'
			else if (B == 30)
				return 'l'
			else if (B == 31)
				return ';'
			else if (B == 32)
				return 'z'
			else if (B == 33)
				return 'x'
			else if (B == 34)
				return 'c'
			else if (B == 35)
				return 'v'
			else if (B == 36)
				return 'b'
			else if (B == 37)
				return 'n'
			else if (B == 38)
				return 'm'
			else if (B == 39)
				return ','
			else if (B == 40)
				return '.'
			else if (B == 41)
				return '/'
			else if (B == 42)
				return 'Q'
			else if (B == 43)
				return 'W'
			else if (B == 44)
				return 'E'
			else if (B == 45)
				return 'R'
			else if (B == 46)
				return 'T'
			else if (B == 47)
				return 'Y'
			else if (B == 48)
				return 'U'
			else if (B == 49)
				return 'I'
			else if (B == 50)
				return 'O'
			else if (B == 51)
				return 'P'
			else if (B == 52)
				return '|'
			else if (B == 53)
				return '-'
			else if (B == 54)
				return '_'
			else if (B == 55)
				return '='
			else if (B == 56)
				return '+'
			else if (B == 57)
				return '*'
			else if (B == 58)
				return '&'
			else if (B == 59)
				return '^'
			else if (B == 60)
				return '%'
			else if (B == 61)
				return '$'
			else if (B == 62)
				return '#'
			else if (B == 63)
				return '@'
			else if (B == 64)
				return '!'
			else if (B == 65)
				return 'A'
			else if (B == 66)
				return 'S'
			else if (B == 67)
				return 'D'
			else if (B == 68)
				return 'F'
			else if (B == 69)
				return 'G'
			else if (B == 70)
				return 'H'
			else if (B == 71)
				return 'J'
			else if (B == 72)
				return 'K'
			else if (B == 73)
				return 'L'
			else if (B == 74)
				return 'Z'
			else if (B == 75)
				return 'X'
			else if (B == 76)
				return 'C'
			else if (B == 77)
				return 'V'
			else if (B == 78)
				return 'B'
			else if (B == 79)
				return 'N'
			else if (B == 80)
				return 'M'
			else if (B == 81)
				return 'й'
			else if (B == 82)
				return 'ц'
			else if (B == 83)
				return 'у'
			else if (B == 84)
				return 'к'
			else if (B == 85)
				return 'е'
			else if (B == 86)
				return 'н'
			else if (B == 87)
				return 'г'
			else if (B == 88)
				return 'ш'
			else if (B == 89)
				return 'щ'
			else if (B == 90)
				return 'з'
			else if (B == 91)
				return 'х'
			else if (B == 92)
				return 'ъ'
			else if (B == 93)
				return 'ф'
			else if (B == 94)
				return 'ы'
			else if (B == 95)
				return 'в'
			else if (B == 96)
				return 'а'
			else if (B == 97)
				return 'п'
			else if (B == 98)
				return 'р'
			else if (B == 99)
				return 'о'
			else if (B == 100)
				return 'л'
			else if (B == 101)
				return 'д'
			else if (B == 102)
				return 'ж'
			else if (B == 103)
				return 'э'
			else if (B == 104)
				return 'я'
			else if (B == 105)
				return 'ч'
			else if (B == 106)
				return 'с'
			else if (B == 107)
				return 'м'
			else if (B == 108)
				return 'и'
			else if (B == 109)
				return 'т'
			else if (B == 110)
				return 'ь'
			else if (B == 111)
				return 'б'
			else if (B == 112)
				return 'ю'
			else if (B == 113)
				return 'ё'
			else if (B == 114)
				return 'Ё'
			else if (B == 115)
				return 'Й'
			else if (B == 116)
				return 'Ц'
			else if (B == 117)
				return 'У'
			else if (B == 118)
				return 'К'
			else if (B == 119)
				return 'Е'
			else if (B == 120)
				return 'Н'
			else if (B == 121)
				return 'Г'
			else if (B == 122)
				return 'Ш'
			else if (B == 123)
				return 'Щ'
			else if (B == 124)
				return 'З'
			else if (B == 125)
				return 'Х'
			else if (B == 126)
				return 'Ъ'
			else if (B == 127)
				return 'Ф'
			else if (B == 128)
				return 'Ы'
			else if (B == 129)
				return 'В'
			else if (B == 130)
				return 'А'
			else if (B == 131)
				return 'П'
			else if (B == 132)
				return 'Р'
			else if (B == 133)
				return 'О'
			else if (B == 134)
				return 'Л'
			else if (B == 135)
				return 'Д'
			else if (B == 136)
				return 'Ж'
			else if (B == 137)
				return 'Э'
			else if (B == 138)
				return 'Я'
			else if (B == 139)
				return 'Ч'
			else if (B == 140)
				return 'С'
			else if (B == 141)
				return 'М'
			else if (B == 142)
				return 'И'
			else if (B == 143)
				return 'Т'
			else if (B == 144)
				return 'Ь'
			else if (B == 145)
				return 'Б'
			else if (B == 146)
				return 'Ю'
			else
				return '-1';
		}
		
		public static function CharToByte(C: String): int
		{
			if (C == '0')
				return 0
			else if (C == '1')
				return 1
			else if (C == '2')
				return 2
			else if (C == '3')
				return 3
			else if (C == '4')
				return 4
			else if (C == '5')
				return 5
			else if (C == '6')
				return 6
			else if (C == '7')
				return 7
			else if (C == '8')
				return 8
			else if (C == '9')
				return 9
			else if (C == 'q')
				return 10
			else if (C == 'w')
				return 11
			else if (C == 'e')
				return 12
			else if (C == 'r')
				return 13
			else if (C == 't')
				return 14
			else if (C == 'y')
				return 15
			else if (C == 'u')
				return 16
			else if (C == 'i')
				return 17
			else if (C == 'o')
				return 18
			else if (C == 'p')
				return 19
			else if (C == '[')
				return 20
			else if (C == ']')
				return 21
			else if (C == 'a')
				return 22
			else if (C == 's')
				return 23
			else if (C == 'd')
				return 24
			else if (C == 'f')
				return 25
			else if (C == 'g')
				return 26
			else if (C == 'h')
				return 27
			else if (C == 'j')
				return 28
			else if (C == 'k')
				return 29
			else if (C == 'l')
				return 30
			else if (C == ';')
				return 31
			else if (C == 'z')
				return 32
			else if (C == 'x')
				return 33
			else if (C == 'c')
				return 34
			else if (C == 'v')
				return 35
			else if (C == 'b')
				return 36
			else if (C == 'n')
				return 37
			else if (C == 'm')
				return 38
			else if (C == ',')
				return 39
			else if (C == '.')
				return 40
			else if (C == '/')
				return 41
			else if (C == 'Q')
				return 42
			else if (C == 'W')
				return 43
			else if (C == 'E')
				return 44
			else if (C == 'R')
				return 45
			else if (C == 'T')
				return 46
			else if (C == 'Y')
				return 47
			else if (C == 'U')
				return 48
			else if (C == 'I')
				return 49
			else if (C == 'O')
				return 50
			else if (C == 'P')
				return 51
			else if (C == '|')
				return 52
			else if (C == '-')
				return 53
			else if (C == '_')
				return 54
			else if (C == '=')
				return 55
			else if (C == '+')
				return 56
			else if (C == '*')
				return 57
			else if (C == '&')
				return 58
			else if (C == '^')
				return 59
			else if (C == '%')
				return 60
			else if (C == '$')
				return 61
			else if (C == '#')
				return 62
			else if (C == '@')
				return 63
			else if (C == '!')
				return 64
			else if (C == 'A')
				return 65
			else if (C == 'S')
				return 66
			else if (C == 'D')
				return 67
			else if (C == 'F')
				return 68
			else if (C == 'G')
				return 69
			else if (C == 'H')
				return 70
			else if (C == 'J')
				return 71
			else if (C == 'K')
				return 72
			else if (C == 'L')
				return 73
			else if (C == 'Z')
				return 74
			else if (C == 'X')
				return 75
			else if (C == 'C')
				return 76
			else if (C == 'V')
				return 77
			else if (C == 'B')
				return 78
			else if (C == 'N')
				return 79
			else if (C == 'M')
				return 80
			else if (C == 'й')
				return 81
			else if (C == 'ц')
				return 82
			else if (C == 'у')
				return 83
			else if (C == 'к')
				return 84
			else if (C == 'е')
				return 85
			else if (C == 'н')
				return 86
			else if (C == 'г')
				return 87
			else if (C == 'ш')
				return 88
			else if (C == 'щ')
				return 89
			else if (C == 'з')
				return 90
			else if (C == 'х')
				return 91
			else if (C == 'ъ')
				return 92
			else if (C == 'ф')
				return 93
			else if (C == 'ы')
				return 94
			else if (C == 'в')
				return 95
			else if (C == 'а')
				return 96
			else if (C == 'п')
				return 97
			else if (C == 'р')
				return 98
			else if (C == 'о')
				return 99
			else if (C == 'л')
				return 100
			else if (C == 'д')
				return 101
			else if (C == 'ж')
				return 102
			else if (C == 'э')
				return 103
			else if (C == 'я')
				return 104
			else if (C == 'ч')
				return 105
			else if (C == 'с')
				return 106
			else if (C == 'м')
				return 107
			else if (C == 'и')
				return 108
			else if (C == 'т')
				return 109
			else if (C == 'ь')
				return 110
			else if (C == 'б')
				return 111
			else if (C == 'ю')
				return 112
			else if (C == 'ё')
				return 113
			else if (C == 'Ё')
				return 114
			else if (C == 'Й')
				return 115
			else if (C == 'Ц')
				return 116
			else if (C == 'У')
				return 117
			else if (C == 'К')
				return 118
			else if (C == 'Е')
				return 119
			else if (C == 'Н')
				return 120
			else if (C == 'Г')
				return 121
			else if (C == 'Ш')
				return 122
			else if (C == 'Щ')
				return 123
			else if (C == 'З')
				return 124
			else if (C == 'Х')
				return 125
			else if (C == 'Ъ')
				return 126
			else if (C == 'Ф')
				return 127
			else if (C == 'Ы')
				return 128
			else if (C == 'В')
				return 129
			else if (C == 'А')
				return 130
			else if (C == 'П')
				return 131
			else if (C == 'Р')
				return 132
			else if (C == 'О')
				return 133
			else if (C == 'Л')
				return 134
			else if (C == 'Д')
				return 135
			else if (C == 'Ж')
				return 136
			else if (C == 'Э')
				return 137
			else if (C == 'Я')
				return 138
			else if (C == 'Ч')
				return 139
			else if (C == 'С')
				return 140
			else if (C == 'М')
				return 141
			else if (C == 'И')
				return 142
			else if (C == 'Т')
				return 143
			else if (C == 'Ь')
				return 144
			else if (C == 'Б')
				return 145
			else if (C == 'Ю')
				return 146
			else
				return -1;
		}
		
		private static function Test(): void
		{
			  var s:String = "";
			  var c: int = 0;
			  for (var i: int = 0; i <= StringUtils.MaxCharCode; ++i)
			  {
				  s = StringUtils.ByteToChar(i);
				  c = StringUtils.CharToByte(s);
				  if (c != i)
					new TModalMessage(s);
			  }
			  new TModalMessage("ok");			
		}
		
		public static function ByteArr2String(Arr: ByteArray): String
		{
			var Res: String = '';
			for (var i: int = 0; i < Arr.length; ++i)
				Res += ByteToChar(Arr[i]).toString();
			trace(Res);
			return Res;
		}
		
		public static function String2ByteArr(S: String, Arr: ByteArray, Size: int): void
		{
			for (var i: int = 0; i < Size; ++i)
			{
				if ((S != null) && (S != "")&&(i < S.length))
					Arr[i] = CharToByte(S.charAt(i))
				else
					Arr[i] = 0;
			}
		}
	}
	
}